package com.abapi.pay.handler;

import cn.hutool.core.lang.ClassScaner;
import com.abapi.pay.core.annotation.EndpointMapping;
import com.abapi.pay.service.AbstractEndpointService;
import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Set;

/**
 * @Author ldx
 * @Date 2019/10/18 11:21
 * @Description
 * @Version 1.0.0
 */
public class HandlerEndpointFactory {

    public static Map<String,AbstractEndpointService> handlerClassMap;

    static {
        Map<String, AbstractEndpointService> handleMap = Maps.newHashMap();
        Set<Class<?>> classes = ClassScaner.scanPackageByAnnotation("com.abapi.pay.service", EndpointMapping.class);
        classes.stream().forEach(e->{
            String type = e.getAnnotation(EndpointMapping.class).value();
            try {
                handleMap.put(type, (AbstractEndpointService) e.newInstance());
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
        });
        handlerClassMap = handleMap;
    }
}
